package com.easy.frame.servers.sys.controller;

import com.easy.frame.base.basic.ServiceResponse;
import com.easy.frame.base.basic.pojo.vo.BaseTreeVO;
import com.easy.frame.base.service.base.BaseController;
import com.easy.frame.servers.sys.dto.SysCategoryTypeDTO;
import com.easy.frame.servers.sys.entity.SysCategoryType;
import com.easy.frame.servers.sys.service.SysCategoryTypeService;
import com.easy.frame.servers.sys.service.SysServerTypeService;
import com.easy.frame.servers.sys.vo.SysCategoryTypeVO;
import com.easy.frame.servers.sys.vo.SysServerTypeVO;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 服务分类表相关接口类
 *
 * @author tp
 * @date 2020/12/31 14:39
 */
@Slf4j
@Api(tags = "服务类型/sys-server")
@RestController
@RequestMapping("/sys-server")
public class SysServerTypeController extends BaseController {

    @Autowired
    SysServerTypeService sysServerTypeService;

    @Autowired
    SysCategoryTypeService sysCategoryTypeService;

    @ApiOperation(value = "树状图-获取服务分类分项树状图")
    @GetMapping("/tree")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "是否只查启用 [0全查 1只查启用]，默认查全部",name = "enabled"),
            @ApiImplicitParam(value = "查询条件，搜索组织树的条件",name = "equName"),
            @ApiImplicitParam(value = "分类类型 [3.服务分类]",name = "type",required = true)})
    public ServiceResponse<? extends BaseTreeVO> tree(@RequestParam(defaultValue = "0") int enabled,
                                                      @RequestParam String equName,
                                                      @RequestParam String type) {
        return success(sysServerTypeService.getEquTypeTree(enabled, equName, type));
    }

    @ApiOperation("点击新增一级分类/新增下级分类")
    @GetMapping("/click/add/ser")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "父级ID，为空时则增加的是一级分项，非空时将在该ID下新增分项",name = "parentEquId"),
            @ApiImplicitParam(value = "分类类型 [3.服务类]",name = "type",required = true)})
    public ServiceResponse<SysCategoryTypeVO> clickAddEqu(@RequestParam Integer parentEquId,
                                                          @RequestParam String type) {
        SysCategoryTypeVO sysCategoryTypeVO = sysServerTypeService.clickAddEqu(parentEquId, type);
        return success(sysCategoryTypeVO);
    }

    @ApiOperation("添加-服务分类分类")
    @PostMapping("/add")
    public ServiceResponse<SysCategoryType> add(@ApiParam("添加能耗分项表单对象")
                                                @RequestBody SysCategoryTypeDTO addDTO) throws Exception {
        //处理父路径参数值，当父值不为空，或者删除头尾为空的时候；
        if (addDTO.getParentPath() != null && ("").equals(addDTO.getParentPath().trim())) {
            addDTO.setParentPath(null);
        }
        return success(sysServerTypeService.add(addDTO));
    }

    @ApiOperation("修改-服务分类分类")
    @PutMapping("/modify")
    public ServiceResponse<SysCategoryType> modify(@ApiParam("服务分类分项实体")
                                                   @RequestBody SysCategoryTypeDTO entity) throws Exception {
        return success(sysServerTypeService.modify(entity));
    }

    @ApiOperation("查询-根据id数据")
    @GetMapping("/by/id")
    public ServiceResponse<?> getById(@ApiParam(value = "主键id", required = true)
                                      @RequestParam Long id) {
        SysServerTypeVO sysServerTypeVO = sysServerTypeService.selectById(id);
        if (StringUtils.isEmpty(sysServerTypeVO)) {
            return fail("未查到设备设施分类详情");
        }
        return success(sysServerTypeVO);
    }

    @ApiOperation("删除-根据id")
    @DeleteMapping("/by/id")
    public ServiceResponse<?> deleteById(@RequestParam Long id) {
        //修改delete字段的值为1
        sysServerTypeService.updateDelete(id);
        return success();
    }

    @ApiOperation("删除父节点-根据id")
    @DeleteMapping("/parent/by/id")
    public ServiceResponse<?> deleteParentById(@RequestParam Long id) {
        //修改delete字段的值为1
        sysServerTypeService.updateParentDelete(id);
        return success();
    }
}
